package xdTool

import sysUtil.BeetlUtil
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdController

class XdWidgetSelectController extends XdController {

    def sysDBUtilService
    def authUtilsService

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        return null
    }

    @Override
    def add() {
        return null
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        return null
    }

    @Override
    def query() {
        def result = ResultJson.resultJson()
        try {
            def keyword = params?.keyword
            def id = params?.id
            def criteria = XdWidgetSelect.createCriteria()
            def results = criteria.list([offset: 0,max: 10]){
                if(keyword){
                    or {
                        eq("label",keyword)
                        eq("name",keyword)
                    }
                }
            }
            def data = []
            results.each {it ->
                def map = [value:it.id,name: "${it.label?:it.name}",selected:(id && "${id}"=="${it.id}")?"selected":""]
                data.add(map)
            }
            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            result.put(ResultJson.DATA.name,data)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,e.getMessage())
            e.printStackTrace()
        }
        render result
    }

    @Override
    def delete() {
        return null
    }

    /**
     * 预览
     */
    def selectView(){
        def result = ResultJson.resultJson()

        try {
            def id = Long.parseLong(params?.id)
            XdWidgetSelect xdWidgetSelect = XdWidgetSelect.get(id)
            def allFlag = params.allFlag?:xdWidgetSelect.allFlag
            def sql = params.dataSql?:SysCommUtil.Clob2String(xdWidgetSelect.dataSql)
            params.valueField = params.valueField?:xdWidgetSelect.valueField
            params.labelField = params.labelField?:xdWidgetSelect.labelField
            params.dataSourceId = params.dataSourceId? Long.parseLong(params.dataSourceId):xdWidgetSelect.dataSourceId


            //处理初始化select值，用于填报
            def initData = []//params?.initData
            if( xdWidgetSelect.radio == "true"   ){
                if( params?.initData){
                    initData = [params?.initData]
                }
            }else {
                if( params?.initData){
                    initData = String.valueOf(params?.initData).split(",")
                }
            }

            sql =  BeetlUtil.getSelectSQLTemplate(params,sql)
            //println sql
            def dataSource = sysDBUtilService.getGroovySqlInstance(XdDataSource.get( params.dataSourceId))
            if(!dataSource){
                dataSource = sysDBUtilService.getGroovySqlInstance()
            }

            def data = []
            if(allFlag == "true"){ //全选
                data = sysDBUtilService.pageAllResult(sql,dataSource)
            }else{
                data = sysDBUtilService.pageResult(sql,1,10,dataSource)
            }


            def results = []
            data.each {it ->
                def map = [:]
                it.each {key,value ->
                    map.put("${String.valueOf(key).toLowerCase()}",value)
                    if( initData.contains(value)   && "${String.valueOf(key).toLowerCase()}" == "value"    ){
                        map.put("selected","selected")
                    }
                }
                results.add(map)
            }

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            result.put(ResultJson.DATA.name,results)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,e.getMessage())
            e.printStackTrace()
        }
        render result
    }
}
